package company.search.beans;

import company.abstractClasses.AbstractCandidateCapableFilterList;
import comparators.CandidateCapableSearchCapableComparator;
import facades.CandidateFacade;
import facades.SearchFacade;
import entities.Candidate;
import entities.Company;
import entities.CompanyFolder;
import entities.Search;
import facades.CompanyFolderTypeFacade;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Observes;
import static javax.enterprise.event.Reception.IF_EXISTS;
import javax.enterprise.event.TransactionPhase;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import predicates.CandidateCapableSearchCapablePredicate;
import qualifiers.ChosenEntityEvent;
import qualifiers.CreatedEntityEvent;
import qualifiers.EditedEntityEvent;

/**
 *
 * @author damfercer
 */
@Named
@SessionScoped
public class NewCreateSearchBean extends AbstractCandidateCapableFilterList<Candidate, CandidateFacade, CandidateCapableSearchCapableComparator<Candidate, CandidateCapableSearchCapablePredicate<Candidate>>, CandidateCapableSearchCapablePredicate<Candidate>> implements Serializable {
    @Inject
    private CompanyFolderTypeFacade companyFolderTypeFacade;
    @Inject
    private SearchFacade searchFacade;
    
    public void onSelectedSearch(@Observes @ChosenEntityEvent Search search){
        setPredicate(new CandidateCapableSearchCapablePredicate(search));
        initSearch();
        //initCandidates();
    }
    
    public void onEditedSearch(@Observes(notifyObserver=IF_EXISTS) @EditedEntityEvent Search search){
       setPredicate(new CandidateCapableSearchCapablePredicate(search));
    }
    
    @Override
    public void initCandidates(){
        setBaseEntityList(getEntityFacade().getFilteredCandidates(""));
        setEntityList(new ArrayList<Candidate>(getBaseEntityList()));
        if(getPredicate()!=null){
            filterFromBaseList();
            sort();
        }
    }
    
    public void onNewCompany(@Observes(during = TransactionPhase.AFTER_SUCCESS) @CreatedEntityEvent Company c){
        c.setSearchList(new ArrayList<Search>());
        c.setCompanyFolderList(new ArrayList<CompanyFolder>());
        FacesContext context = FacesContext.getCurrentInstance();
        Locale locale=context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        String testSearch=rb.getString("TestSearch");
        getPredicate().getSearch().setDescription(testSearch);
        getPredicate().getSearch().setActive(Boolean.TRUE);
        getPredicate().getSearch().setIsPublic(Boolean.FALSE);
        getPredicate().getSearch().setRequiredVehicle(Boolean.FALSE);
        getPredicate().getSearch().setCreateDate(new Date());
        getPredicate().getSearch().setLastSearchDate(new Date());
        getPredicate().getSearch().setFolder(new CompanyFolder());
        getPredicate().getSearch().getFolder().setIdCompany(c);
        getPredicate().getSearch().getFolder().setSearchList(new ArrayList<Search>());
        getPredicate().getSearch().getFolder().getSearchList().add(getPredicate().getSearch());
        getPredicate().getSearch().getFolder().setCompanyFolderType(companyFolderTypeFacade.getNormalFolderType());
        getPredicate().getSearch().getFolder().setDescription(testSearch);
        getPredicate().getSearch().getFolder().setCreationDate(new Date());
        searchFacade.create(getPredicate().getSearch());
        c.getSearchList().add(getPredicate().getSearch());
        c.getCompanyFolderList().add(getPredicate().getSearch().getFolder());
        setCompany(c);
    }    

}
